package com.asl.website.sysAboutContact.service.impl;

import java.util.List;
import com.asl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.asl.website.sysAboutContact.mapper.SysAboutContactMapper;
import com.asl.website.sysAboutContact.domain.SysAboutContact;
import com.asl.website.sysAboutContact.service.ISysAboutContactService;

/**
 * 联系我们Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
public class SysAboutContactServiceImpl implements ISysAboutContactService 
{
    @Autowired
    private SysAboutContactMapper sysAboutContactMapper;

    @Autowired
    private EmailService emailService;

    /**
     * 查询联系我们
     * 
     * @param id 联系我们主键
     * @return 联系我们
     */
    @Override
    public SysAboutContact selectSysAboutContactById(Long id)
    {
        return sysAboutContactMapper.selectSysAboutContactById(id);
    }

    /**
     * 查询联系我们列表
     * 
     * @param sysAboutContact 联系我们
     * @return 联系我们
     */
    @Override
    public List<SysAboutContact> selectSysAboutContactList(SysAboutContact sysAboutContact)
    {
        return sysAboutContactMapper.selectSysAboutContactList(sysAboutContact);
    }

    /**
     * 新增联系我们
     * 
     * @param sysAboutContact 联系我们
     * @return 结果
     */
    @Override
    public int insertSysAboutContact(SysAboutContact sysAboutContact)
    {
        // 1. 设置创建时间为当前时间
        sysAboutContact.setCreateTime(DateUtils.getNowDate());

        // 2. 执行数据库插入
        int rows = sysAboutContactMapper.insertSysAboutContact(sysAboutContact);

        // 3. 如果插入成功且配置了邮件服务，则发送邮件通知
        if (rows > 0 && emailService != null) {
            try {
                // 异步发送邮件，避免阻塞主线程
                new Thread(() -> {
                    try {
                        emailService.sendContactEmail(sysAboutContact);
                    } catch (Exception e) {
                        // 记录异常但不中断业务
                        System.out.println("发送联系信息邮件失败，数据ID："+
                                sysAboutContact.getId()+"，错误："+e.getMessage()+e);
                    }
                }).start();
            } catch (Exception e) {
                // 线程启动异常处理
                System.out.println("启动邮件发送线程失败，数据ID："+
                        sysAboutContact.getId()+ "，错误："+e.getMessage()+e);
            }
        }
        return rows;
    }

    /**
     * 修改联系我们
     * 
     * @param sysAboutContact 联系我们
     * @return 结果
     */
    @Override
    public int updateSysAboutContact(SysAboutContact sysAboutContact)
    {
        sysAboutContact.setUpdateTime(DateUtils.getNowDate());
        return sysAboutContactMapper.updateSysAboutContact(sysAboutContact);
    }

    /**
     * 批量删除联系我们
     * 
     * @param ids 需要删除的联系我们主键
     * @return 结果
     */
    @Override
    public int deleteSysAboutContactByIds(Long[] ids)
    {
        return sysAboutContactMapper.deleteSysAboutContactByIds(ids);
    }

    /**
     * 删除联系我们信息
     * 
     * @param id 联系我们主键
     * @return 结果
     */
    @Override
    public int deleteSysAboutContactById(Long id)
    {
        return sysAboutContactMapper.deleteSysAboutContactById(id);
    }
}
